package work.shyk.kongMonitor.dto.result.interfaces;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.Map;

/**
 * description: <br>
 * createTime: 2021/3/25 9:49 <br>
 *
 * @author zzh
 */
public interface ValueReceiver {
    default void receiveValue(String field, Object obj) {
        if(this instanceof Map) {
            ((Map)this).put(field, obj);
            return;
        }
        Class<? extends ValueReceiver> aClass = this.getClass();
        String setMethodName = "set" + field.substring(0, 1).toUpperCase(Locale.ROOT) + field.substring(1);
        try {
            for (Method method : aClass.getMethods()) {
                if(method.getName().equals(setMethodName)) {
                    method.invoke(this, obj);
                }
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
